package com.jlastu.znb.view;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import com.jlastu.znb.R;
import com.jlastu.znb.buy.buyManager;
import com.jlastu.znb.contract.MineContract;
import com.jlastu.znb.database.DBHelper;
import com.jlastu.znb.dfh.dfhManager;
import com.jlastu.znb.entity.Goods;
import com.jlastu.znb.entity.Photo;
import com.jlastu.znb.gwc.CartManager;
import com.jlastu.znb.sc.scManager;
import com.jlastu.znb.utils.SpUtils;

import java.util.List;

public class fkActivity extends AppCompatActivity {

    private DBHelper dbHelper;
    private int goodsId;
    private CartManager cartManager;
    private scManager scManager;
    private buyManager buyManager;
    private dfhManager dfhManager;
    private SpUtils mSpUtils;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.zhifu);
        mSpUtils = new SpUtils(this);
        cartManager = new CartManager(this);
        scManager = new scManager(this);
        buyManager = new buyManager(this);
        dbHelper = new DBHelper(this);
        dfhManager = new dfhManager(this);
        goodsId = getIntent().getIntExtra("GOODS_ID", -1);
        //设置商品详情信息
        if (goodsId != -1) {
            Goods goods = dbHelper.getGoodsById(goodsId);
            if (goods != null) {
                TextView nameView = findViewById(R.id.item_name56);
                TextView priceView = findViewById(R.id.item_price56);
                TextView messageView= findViewById(R.id.message56);
                ImageView imageView = findViewById(R.id.item_image56);
                List<Photo> photos = goods.getPhotoList();
                if (photos != null && !photos.isEmpty()) {
                    Photo firstPhoto = photos.get(0);
                    Bitmap bitmap = BitmapFactory.decodeByteArray(firstPhoto.getData(), 0, firstPhoto.getData().length);
                    imageView.setImageBitmap(bitmap);
                }
                messageView.setText(goods.getMessage());
                nameView.setText(goods.getName());
                priceView.setText(String.format("¥ %s/%s", goods.getPrice(), goods.getUnit()));
            }
        }
        Button lkgm =findViewById(R.id.lkgm);
        lkgm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int userId = mSpUtils.getInt(SpUtils.LOGIN_KEY, -1);
                if (goodsId != -1) {
                    handlePurchase(goodsId);
                    dfhManager.addTodfh(goodsId, 1, userId);
                    Toast.makeText(fkActivity.this, "支付成功,返回主页", Toast.LENGTH_SHORT).show();
                    Intent intent=new Intent(fkActivity.this,MainActivity.class);
                    startActivity(intent);
                }
            }
        });
    }

    private void handlePurchase(int goodsId) {
        buyManager.removeFromBuyByGoodsId(goodsId);
        Intent intent = new Intent("com.jlastu.znb.REFRESH_CART");
        LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
    }
    private int getUserId() {
        return mSpUtils.getInt(SpUtils.LOGIN_KEY, -1);
    }

}